import Taro, { Component } from '@tarojs/taro'
import { View, Image } from '@tarojs/components'
import { Tabbar, Footer, MoreFormid, DataLoading, Modal } from './../../../component/index'
import allRequest from '../../../api/allRequest';
import './my.less'

let _this

let enableDebugCounter = 0
class Index extends Component {

  config = {
    navigationBarTitleText: '企业中心'
  }

  componentWillReceiveProps(nextProps) {
    console.log(this.props, nextProps)
  }

  constructor() {
    super()
    Taro.setNavigationBarColor({
      frontColor: Taro.getStorageSync('secondColor'),
      backgroundColor: Taro.getStorageSync('baseColor')
    })
    this.state = {
      avatarUrl: 'https://file.open.banchengyun.com/2019/03/28/155375292386271.png',
      nick_name: '',
      storeName: '',
      list: [],
      myInfo: {},
      userInfo: {},
      company_protocol_title: "",
      changeLoad: true,
      modalInfo: {
        title: '',
        conOne: '',
        conSec: '',
        btnInfo: '确认',
        type: 0, //1余额充足  0余额不足
      },
      member_right: {},
    }
  }

  componentDidMount() {
    _this = this

  }

  componentWillUnmount() { }

  componentDidShow() {
    this.getData()
    //缓存用户处于企业端
    Taro.setStorageSync('situation', 'business')
    //获取微信信息，缓存获取
    let userInfo = Taro.getStorageSync("user")
    userInfo = JSON.parse(userInfo)
    this.setState({ userInfo })
    //获取协议名称
    this.setState({
      company_protocol_title: JSON.parse(Taro.getStorageSync("saasConfig")).company_protocol_title
    })
  }

  componentDidHide() { }

  getData() {
    allRequest.companyMe()
      .then(res => {
        let { member_right } = res
        this.setState({ myInfo: res, member_right }, () => {
          setTimeout(() => { this.setState({ changeLoad: false }) }, 100)
        })
      })
      .catch(err => { console.log(err) })
  }

  enableDebug(e) {
    if (++enableDebugCounter > 10) {
      Taro.setEnableDebug({
        enableDebug: true
      })
    }
  }

  loginRefreshPage() {
    this.getData()
  }

  toWhere(url, type, e) {
    if (type) {
      Taro[type]({
        url
      })
    }
  }

  exitModal() {
    _this.setState({
      isModal: false,
      comfirmType: ''
    })
  }

  confirmModal() {
    let { comfirmType, member_right } = _this.state
    if (comfirmType == 'publicJob') {
      if (member_right.publish_job_times > 0) {
        _this.exitModal()
        Taro.navigateTo({
          url: `/subPackages/subPackageF/publicJob/publicJob`
        })
      } else {
        _this.exitModal()
        Taro.navigateTo({
          url: '/subPackages/subPackageF/equity/equity'
        })
      }
    }
  }

  equityTip() {
    let { modalInfo, member_right } = this.state
    if (member_right.publish_job_times > 0) {
      modalInfo = {
        title: `是否扣除1次发布职位权益`,
        conOne: `当前可免费发布${member_right.publish_job_times}次，发布成功后即会扣除`,
        btnInfo: '确认',
        type: 1,
      }
    } else {
      modalInfo = {
        title: `当前可发布职位0次`,
        conSec: '是否前往我的权益购买',
        btnInfo: '去购买',
        type: 0, //1余额充足  0余额不足
      }
    }
    this.setState({ isModal: true, modalInfo, comfirmType: 'publicJob' })
  }


  render() {
    const currentPage = 'businessMy';
    const { avatarUrl, myInfo, userInfo, company_protocol_title, changeLoad, isModal, modalInfo, member_right } = this.state;
    if (Object.keys(myInfo).length) {
      return (
        <View className='businessMy'>
          <View className='header' style={`background:${Taro.getStorageSync('baseColor')}`}>
            <View className='imgBack'>
              <View className='content'>
                <View className='titleBlock'>
                  <View className='title'>{userInfo.nick_name}</View>
                  {/* <Image className='icon' src=''></Image> */}
                </View>
                <View className='nextTitleBlock'>
                  <View className='nextTitle'>{myInfo.company_principal.company.title}</View>
                  <Image className='icon' src='https://file.open.banchengyun.com/353/361a8d65fb06ea02cbac7ab774e814fb'></Image>
                </View>
                <View className='tip'>(id:{myInfo.company_principal.company_id})</View>
                <View className='number'>{userInfo.phone}</View>
              </View>
              <View className='headAvatar'>
                <Image src={myInfo.company_principal.company.logo || userInfo.avatar_url || avatarUrl} onClick={this.enableDebug.bind(this)} className='avatar' mode='aspectFill'></Image>
              </View>
            </View>
          </View>
          <View className='some'>
            <View className='someBlock'>
              <View className='blockOne'>
                <View className='oneItem' onClick={this.equityTip.bind(this)}>
                  <Image className='oneImg' src='https://file.open.banchengyun.com/353/a4e8d0294a73c65bffd9875de40567bc'></Image>
                  <View className='oneCon'>发布职位</View>
                </View>
                <View className='oneItem' onClick={this.toWhere.bind(this, `/subPackages/subPackageF/jobManage/jobManage?auth_status=${myInfo.company_auth ? myInfo.company_auth.auth_status : 0}`, 'navigateTo')}>
                  <Image className='oneImg' src='https://file.open.banchengyun.com/353/6429b8dc4db90b9c313e399a1d940f38'></Image>
                  <View className='oneCon'>职位管理</View>
                </View>
                <View className='oneItem' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/balancePhone/balancePhone', 'navigateTo')}>
                  <Image className='oneImg' src='https://file.open.banchengyun.com/353/185d15430ee8892f45fa0d6aa4b23dfe'></Image>
                  <View className='oneCon'>已购简历</View>
                </View>
                <View className='oneItem' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/myShop/myShop', 'navigateTo')}>
                  <Image className='oneImg' src='https://file.open.banchengyun.com/353/872efad64eed41ec9064a247fdda659d'></Image>
                  <View className='oneCon'>公司信息</View>
                </View>
              </View>
              <View className='line'></View>
              <View className='blockTwo'>
                <View className='twoItem'>
                  <View className='twoTitle'>职位发布</View>
                  <View className='twoCon'><View style='font-size:18px;'>{member_right.publish_job_times}</View>次/职位</View>
                </View>
                <View className='twoItem'>
                  <View className='twoTitle'>职位刷新</View>
                  <View className='twoCon'><View style='font-size:18px;'>{member_right.refresh_job_times}</View>次/职位</View>
                </View>
                <View className='twoItem'>
                  <View className='twoTitle'>免费简历</View>
                  <View className='twoCon'><View style='font-size:18px;'>{member_right.free_resume_times}</View>份/简历</View>
                </View>
                <View className='twoBtn' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/equity/equity', 'navigateTo')} style={`background:${Taro.getStorageSync('baseColor')}`}>查看特权</View>
              </View>
            </View>
          </View>
          <View className='whereBlock'>
            <View className='toItem' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/balanceInfo/balanceInfo', 'navigateTo')}>
              <View className='toIcon' style={`background:${Taro.getStorageSync('baseColor')}`}>
                <Image src='https://file.open.banchengyun.com/353/2a7f5f8db80a797c4ff1c4f43221a4db' className='icon'></Image>
              </View>
              <View className='toContent'>
                <View className='name'>企业账户</View>
                <View className='others'>
                  <View className='more'>余额：{myInfo.company_principal.company.balance}元</View>
                  <View className='right-icon'></View>
                </View>
              </View>
            </View>
            <View className='toItem' onClick={this.toWhere.bind(this, '/subPackages/subPackageF/shopApprove/shopApprove', 'navigateTo')}>
              <View className='toIcon' style={`background:${Taro.getStorageSync('baseColor')}`}>
                <Image src='https://file.open.banchengyun.com/353/d070f73d0f1024241188c54ec9407f90' className='icon'></Image>
              </View>
              <View className='toContent'>
                <View className='name'>审核状态</View>
                <View className='others'>
                  {myInfo.company_auth == null && <View className='more'>提交认证</View>}
                  {myInfo.company_auth != null && myInfo.company_auth.auth_status == 0 && myInfo.company_auth.auth_image && <View className='more'>审核中</View>}
                  {myInfo.company_auth != null && myInfo.company_auth.auth_status == 1 && myInfo.company_auth.auth_image && <View className='more'>审核通过</View>}
                  {myInfo.company_auth != null && myInfo.company_auth.auth_status == 2 && myInfo.company_auth.auth_image && <View className='more'>认证失败，重新认证</View>}
                  <View className='right-icon'></View>
                </View>
              </View>
            </View>
            {/* <View className='toItem'>
              <View className='toIcon' style={`background:${Taro.getStorageSync('baseColor')}`}>
                <Image src='https://file.open.banchengyun.com/353/591ff7b64461fd88ef2f79849a29e725' className='icon'></Image>
              </View>
              <View className='toContent'>
                <View className='name'>设置</View>
                <View className='others'>
                  <View className='right-icon'></View>
                </View>
              </View>
            </View> */}
          </View>
          <View className='whereBlock'>
            <View className='toItem' onClick={this.toWhere.bind(this, '/pages/consumer/index/index', 'redirectTo')}>
              <View className='toIcon' style={`background:${Taro.getStorageSync('baseColor')}`}>
                <Image src='https://file.open.banchengyun.com/353/6da8c3516baf0ed28cc79cd41294ccb9' className='icon'></Image>
              </View>
              <View className='toContent'>
                <View className='name'>切换至我要求职</View>
                <View className='others'>
                  {/* <View className='more'>余额：100元</View> */}
                  <View className='right-icon'></View>
                </View>
              </View>
            </View>
          </View>
          <View className='protocol'>
            我已阅读并同意<View className='proName' style={`color:${Taro.getStorageSync('baseColor')}`} onClick={this.toWhere.bind(this, '/subPackages/subPackageS/protocol/protocol?type=company', 'navigateTo')}>《{company_protocol_title}》</View>
          </View>
          <View className='footer'>
            <Footer></Footer>
          </View>
          <Tabbar currentPage={currentPage}></Tabbar>
          {changeLoad && <DataLoading></DataLoading>}
          {isModal && <Modal onExitModal={this.exitModal} modalInfo={modalInfo} onConfirm={this.confirmModal}></Modal>}
        </View >
      )
    }

  }
}

export default Index
